﻿
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour {

    #region 单例模式
    private static GameManager _instance;
    public static GameManager Instance {
        get {
            if (_instance == null) {
                _instance = GameObject.Find("GameManager").GetComponent<GameManager>();
            }
            return _instance;
        }
    }
    #endregion

    /// <summary>
    /// 是否游戏结束
    /// </summary>
    public bool IsGameOver = false;
    public System.Random Random = new System.Random();
    private GlobalData GlobalData;

    // Use this for initialization
    void Start () {
        this.GlobalData = GlobalData.Instance;
        GameObject.DontDestroyOnLoad(this);
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    public void KillTrigger(string triggerName) {
        this.GlobalData.ScenePersonageInfo[this.GlobalData.SceneName][triggerName].Exists = false;
    }

    public void KillTrigger() {
        //print(this.TriggerName);
        this.KillTrigger(this.GlobalData.TriggerName);
    }

    public void UpdateModelInfo(string name, Transform transform) {
        this.UpdateModelInfo(name, transform.position, transform.eulerAngles);
    }

    public void UpdateModelInfo(string name, Vector3 position, Vector3 eulerAngles) {
        this.GlobalData.ScenePersonageInfo[this.GlobalData.SceneName][name].Update(position, eulerAngles);
    }

    public void UpdatePlayer(Transform transform) {
        this.UpdateModelInfo("Player", transform);
    }

    public void UpdatePlayer(Vector3 position, Vector3 eulerAngles) {
        this.UpdateModelInfo("Player", position, eulerAngles);
    }

    public void SaveData() {
        //Dictionary<string, ModelInfo> keyValues = new Dictionary<string, ModelInfo>();
        /**
         * 保存场景人物信息
         */
        //Transform[] transforms = GameObject.Find("Personages").GetComponentsInChildren<Transform>();
        //foreach (Transform transform in transforms) {
        //    if (transform.name.Equals("Personages")) {
        //        continue;
        //    }
        //    print("savedata: " + transform.name);
        //    ModelInfo modelInfo = new ModelInfo(transform);
        //    modelInfo.name = transform.gameObject.name;
        //    modelInfo.Exists = transform.gameObject.activeSelf;
        //    keyValues.Add(modelInfo.name, modelInfo);
        //}
        //this.PersonageInfo = keyValues;

        /**
         * 保存场景名称
         */
        this.GlobalData.SceneName = SceneManager.GetActiveScene().name;

        /**
         * 保存角色当前位置信息
         */
        Transform player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
        this.UpdatePlayer(player);

        //string personageString = JsonHelper.ToJsonString(this.GlobalData.PersonageInfo);
        //PlayerPrefs.SetString("personage", personageString);
        ///**
        // * 保存队伍信息
        // */
        //string ourTeamString = JsonHelper.ToJsonString(this.GlobalData.OurTeam);
        //PlayerPrefs.SetString("ourTeam", ourTeamString);

        //print(personageString);
        //print(ourTeamString);
        this.GlobalData.SaveData();
    }

    public void LoadData(int archiveIndex) {
        //string data = PlayerPrefs.GetString("personage", null);
        //if (data != null) {
        //    this.GlobalData.PersonageInfo = JsonHelper.ParseObject<Dictionary<string, ModelInfo>>(data);
        //}

        //string ourTeamString = PlayerPrefs.GetString("ourTeam", null);
        //if (ourTeamString != null) {
        //    this.GlobalData.OurTeam = JsonHelper.ParseObject<List<Personage>>(ourTeamString);
        //}
        this.GlobalData.LoadData(archiveIndex);
    }

    public void SaveScene() {
        
    }

    public void SaveGame() {
        this.SaveData();
    }

    public void LoadScene() {

    }

    public void LoadGame() {
        LoadingPanel loadingPanel = GameObject.Find("LoadingPanel").GetComponent<LoadingPanel>();
        this.GlobalData.LoadData(0);
        loadingPanel.Show();
        loadingPanel.LoadScene("Init");
    }

    public void NewGame() {
        LoadingPanel loadingPanel = GameObject.Find("LoadingPanel").GetComponent<LoadingPanel>();
        this.GlobalData.InitData();
        loadingPanel.Show();
        loadingPanel.LoadScene("Init");
    }

    public void BackToMenu() {
        LoadingPanel loadingPanel = GameObject.Find("LoadingPanel").GetComponent<LoadingPanel>();
        loadingPanel.Show();
        loadingPanel.LoadScene("Menu");
    }

    public void ExitGame() {
        #if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
        #else
        Application.Quit();
        #endif
    }
}
